package com.I9lou.se.lurker.analyzer;

import java.io.IOException;
import java.io.Reader;

import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;

public class CommaTokenizer extends Tokenizer {

	private CharTermAttribute	charTermAttribute	= addAttribute(CharTermAttribute.class);

	public CommaTokenizer(Reader input) {
		super(input);
	}

	/**
	 * 根据逗号分词
	 */
	@Override
	public boolean incrementToken() throws IOException {
		clearAttributes();
		StringBuilder sb = new StringBuilder();
		int c;
		while ((c = input.read()) != -1) {
			if (c == ',') {
				charTermAttribute.append(sb);
				return true;
			}
			sb.append((char) c);
		}
		if (sb.length() > 0) {
			charTermAttribute.append(sb);
			return true;
		}
		return false;
	}

}
